package cn.edu.fzu.delivery.controller.sys;

import cn.edu.fzu.delivery.common.PageVO;
import cn.edu.fzu.delivery.common.Result;
import cn.edu.fzu.delivery.common.RetCode;
import cn.edu.fzu.delivery.domain.entity.Customer;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.domain.enums.SexEnum;
import cn.edu.fzu.delivery.domain.enums.UserStatusEnum;
import cn.edu.fzu.delivery.domain.enums.UserTypeEnum;
import cn.edu.fzu.delivery.service.UserService;
import cn.edu.fzu.delivery.session.SessionConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author fangxin
 * @date 2022/11/3
 */
@EnableOpenApi
@RestController
@RequestMapping("/api")
@Api(tags = "人员管理")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/auth/sys/user/list")
    @ApiOperation(value = "系统管理员-获取系统人员接口", httpMethod = "GET", notes = "获取系统人员接口")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true),
            @ApiImplicitParam(name = "type", value = "1-快递员，2-网点管理员，3-超级管理员"),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常")})
    public Result list(Long networkId, String name, UserTypeEnum type, UserStatusEnum status, String phone,
                       String email, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        try (Page<SysUser> page = PageHelper.startPage(pageNum, pageSize)) {
            userService.listSysUser(networkId, name, type, status, phone, email, currentUser);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }

    @PostMapping("/auth/sys/user/update")
    @ApiOperation(value = "系统管理员-更新系统人员信息接口", httpMethod = "POST", notes = "更新系统人员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true),
            @ApiImplicitParam(name = "type", value = "1-快递员，2-网点管理员，3-超级管理员"),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常"),
            @ApiImplicitParam(name = "sex", value = "-1-保密，0-男，1-女，2-其他")})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result update(Long id, String email, Long networkId, String name, SexEnum sex, Date birth, String phone,
                         String address, UserTypeEnum type, UserStatusEnum status,
                         HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        userService.updateSysUserInfo(id, email, name, sex, birth, phone, address, type, status, networkId,
                currentUser);
        return new Result(RetCode.SUCCESS, "修改成功");
    }

    @PostMapping("/auth/sys/user/add")
    @ApiOperation(value = "系统管理员-添加系统人员接口", httpMethod = "POST", notes = "添加系统人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", required = true),
            @ApiImplicitParam(name = "name", required = true),
            @ApiImplicitParam(name = "type", value = "1-快递员，2-网点管理员，3-超级管理员", required = true),
            @ApiImplicitParam(name = "networkId", value = "所属网点Id"),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常"),
            @ApiImplicitParam(name = "sex", value = "-1-保密，0-男，1-女，2-其他")})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result add(String email, String name, SexEnum sex, Date birth, String phone, String address,
                      UserTypeEnum type, Long networkId, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        userService.addSysUser(email, name, sex, birth, phone, address, type, networkId, currentUser);
        return new Result(RetCode.SUCCESS, "添加成功");
    }

    @PostMapping("/auth/sys/user/change")
    @ApiOperation(value = "系统管理员-更新系统人员状态接口", httpMethod = "POST", notes = "更新系统人员状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常")})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result changeStatus(Long id, UserStatusEnum status, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        userService.updateSysUserStatus(id, status, currentUser);
        return new Result(RetCode.SUCCESS, (status == UserStatusEnum.DELETE) ? "删除成功" : "修改成功");
    }

    @GetMapping("/auth/sys/user/listCustomer")
    @ApiOperation(value = "系统管理员-查询顾客列表", httpMethod = "GET", notes = "查询顾客列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "email", value = "邮箱"),
            @ApiImplicitParam(name = "sex", value = "-1-保密，0-男，1-女，2-其他"),
            @ApiImplicitParam(name = "phone", value = "联系方式"),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true)})
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result listCustomer(String name, String email, SexEnum sex, String phone, UserStatusEnum status,
                               Integer pageNum, Integer pageSize, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        try (Page<Customer> page = PageHelper.startPage(pageNum, pageSize)) {
            userService.listCustomer(name, email, sex, phone, status, currentUser);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }

    @PostMapping("/auth/sys/user/updateCustomerStatus")
    @ApiOperation(value = "系统管理员-修改顾客状态", httpMethod = "POST", notes = "修改顾客状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true),
            @ApiImplicitParam(name = "status", value = "-1-删除，0-禁用，1-正常", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateCustomerStatus(Long id, UserStatusEnum status, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        userService.updateCustomerStatus(id, status, currentUser);
        return new Result((status == UserStatusEnum.DELETE) ? "删除成功" : "修改成功");
    }

    @PostMapping("/user/updateImage")
    @ApiOperation(value = "更新登录用户的头像", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "imageUrl", value = "头像url") })
    public Result updateImageUrl(String imageUrl, HttpServletRequest request) {
        Object currentUser = request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        userService.updateImageUrl(imageUrl, currentUser);
        return new Result("头像修改成功");
    }
}
